import os

from ultralytics import YOLO


def main(img_file_path):
    model_path = "apps/Model/yolo/best.pt"

    model = YOLO(model_path)
    results = model(img_file_path)
    try:
        for result in results:
            boxes = result.boxes
            # print(f"probs:{boxes.conf}")
            # print(f"position:{boxes.xyxy}")
            # print(f"name:{result.names[boxes.cls.cpu().numpy()[0]]}")

            save_path = "/media"
            file_name = "/result.jpg"
            result.save(filename=file_name)

        if result.names[boxes.cls.cpu().numpy()[0]]:
            # 结果图片的存储路径
            result_path = os.path.join(save_path, file_name)
            print(f"save at {result_path}")
            print(boxes.conf.item(), boxes.xyxy.tolist()[0], result.names[boxes.cls.cpu().numpy()[0]])
            return result_path, boxes.conf.item(), boxes.xyxy.tolist()[0], result.names[boxes.cls.cpu().numpy()[0]]
        else:
            return "no detection"
    except Exception as e:
        return "no detection"
